public class Text6 {
    public int lengthOfLastWord(String s) {
        if(s == null){
            return 0;
        }

        char[] str = s.toCharArray();
        int tmp = 0;
        int num = 0;
        for(int i = 0; i < str.length; i++){
            if(str[i] == ' '){
                if(num != 0){
                    tmp = num;
                }
                num = 0;
            } else {
                num++;
            }
        }

        if(num == 0){
            num = tmp;
        }

        return num;
    }
}
